package com.cpi.cornpp.futuresversions.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.futurepurchase.dto.CustomerFuturePurchaseDTO;
import com.cpi.cornpp.futurepurchase.view.CustomerFuturePurchaseView;
import com.cpi.cornpp.futurepurchase.view.FuturePurchaseView;
import com.cs.common.utils.ArrayListNoRepeat;

public final class CustomerFuturePurchaseConverter {
	public static List convertDTOList(List dtoList){
		boolean existView = false;
		CustomerFuturePurchaseView view = null;
		CustomerFuturePurchaseView auxView = null;
		CustomerFuturePurchaseDTO dto = null;
		FuturePurchaseView fupView = null;
		List viewList = null;
		int antfutureDistributionId = -1;
		int int_i = 0;
		
		viewList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(dtoList)){
			Iterator itr = dtoList.iterator();
			while ( itr.hasNext() ){
				existView = false;
				fupView = new FuturePurchaseView();
				view = new CustomerFuturePurchaseView();
				dto = (CustomerFuturePurchaseDTO) itr.next();
				
				view.setCustomerId(dto.getCustomerId());
				view.setDescription(dto.getCustomerDescription());
				
				antfutureDistributionId = dto.getCornFutureDistributionId().intValue();
				fupView.setCornFutureDistributionId(antfutureDistributionId);
				fupView.setFutureTypeId(dto.getFutureType());
				fupView.setCustomerFutureQuantity(dto.getCustomerQuantity());
				fupView.setAccountFuturePeriodName(dto.getFuturePurchaseAccountPeriod());
				fupView.setApplicationFuturePeriodName(dto.getFuturePurchaseApplicationPeriod());
				fupView.setQuantity(dto.getFuturePurchaseQuantity().intValue());
				fupView.setUnitPrice(dto.getFuturePurchaseUnitPrice().doubleValue());
				fupView.setFuturePurchaseId(dto.getFuturePurchaseId().intValue());
				
				if (CollectionUtils.isNotEmpty(viewList)){
					Iterator itrView = viewList.iterator();
					while (itrView.hasNext()){
						auxView = (CustomerFuturePurchaseView) itrView.next();
						
						if (auxView.getCustomerId().equalsIgnoreCase(view.getCustomerId())){
							
							if (CollectionUtils.isEmpty(auxView.getFuturePurchases())){
								auxView.setFuturePurchases(new ArrayListNoRepeat(0));
							}
							
							auxView.getFuturePurchases().add(fupView);
							existView = true;
							break;
						}
					}
					
					if (!existView){
						view.setFuturePurchases(new ArrayListNoRepeat(0));
						view.getFuturePurchases().add(fupView);
						viewList.add(view);
						int_i++;
					}
				} else {
					view.setFuturePurchases(new ArrayListNoRepeat(0));
					if( int_i == 0 ){
						view.getFuturePurchases().add(fupView);
					}else{
						if(antfutureDistributionId != dto.getCornFutureDistributionId().intValue()){
							view.getFuturePurchases().add(fupView);
						}
					}
					viewList.add(view);
					int_i++;
				}
			}
		}
		
		return viewList;
	}
}